﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SlimDX;
using SlimDX.Direct3D9;

namespace SlimWater
{
    class Terrain
    {
        Device device;

        XMesh mesh;
        Effect effect;

        Matrix world;

        public Terrain(Device device, float scale)
        {
            this.device = device;
            mesh = new XMesh(device, "terrain.x");
            world = Matrix.Scaling(scale, 2 * scale, scale) * Matrix.Translation(0, -5, 0);

            effect = Effect.FromFile(device, "../../data/terrain.fx", ShaderFlags.None);
        }

        public void Render()
        {
            Matrix wvp = world * device.GetTransform(TransformState.View) * device.GetTransform(TransformState.Projection);

            device.SetStreamSource(0, mesh.mesh.VertexBuffer, 0, mesh.mesh.BytesPerVertex);
            device.Indices = mesh.mesh.IndexBuffer;
            device.VertexFormat = mesh.mesh.VertexFormat;

            effect.Technique = "TerrainTechnique";
            effect.SetValue("xWorldViewProj", wvp);
            effect.SetTexture("xTerrainTexture", mesh.textures[0]);

            effect.Begin(0);
            effect.BeginPass(0);
            {
                device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, mesh.mesh.VertexCount, 0, mesh.mesh.FaceCount);
            }
            effect.EndPass();
            effect.End();
        }
    }
}
